package com.lemt.mall.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.lemt.common.result.Res;
import com.lemt.mall.entity.Category;
import com.lemt.mall.entity.Goods;
import com.lemt.mall.entity.Inventory;
import com.lemt.mall.entity.vo.GoodsVo;
import com.lemt.mall.service.IGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Update;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;

import java.util.List;
import java.util.Map;

import static org.springframework.beans.BeanUtils.copyProperties;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zik
 * @since 2023-09-10
 */
@Slf4j
@Api(tags = "货物操作")
@RestController
@RequestMapping("/mall/goods")
public class GoodsController {

    @Resource
    private IGoodsService goodsService;


    @ApiOperation("添加货物")
    @PostMapping("/saveGoods")
    public Res<Boolean> saveGoods(@RequestBody GoodsVo goodsVo){

        if(goodsService.saveGoods(goodsVo)){
            return Res.ok(true);
        }
        return Res.fail(false);
    }

    @ApiOperation("根据id删除商品")
    @DeleteMapping("/remove/{id}")
    public Res<Boolean> removeGoods(@PathVariable Integer id){
       String flag =  goodsService.removeGoodsById(id);
       if (flag.equals("删除成功")){
           return Res.ok(true);
       }
       return Res.fail(false);
    }

    @ApiOperation("修改货物信息")
    @PostMapping("/updateGoods")
    public Res<Boolean> updateGoods(@RequestBody Goods goods) {
        return Res.ok(goodsService.updateById(goods));
    }


    @ApiOperation("商品分页")
    @GetMapping("/getPage")
    public Res<Map<String, Object>> list(@RequestParam Integer page, @RequestParam Integer pageSize, @RequestParam(defaultValue = "") String name){
        log.info("{}{}{}",page,pageSize,name);
        return Res.ok(goodsService.getList(page,pageSize,name));
    }





}
